package com.fivefu.core.camera.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.vo.ResultInfo;

import com.fivefu.core.camera.services.DbMonitorCameraService;
import com.fivefu.core.camera.services.MonitorCameraService;
import com.fivefu.core.camera.vo.request.DbMonitorCameraVo;

import com.fivefu.core.camera.vo.request.ProSourcePage;
import com.fivefu.core.camera.vo.response.DetailProbe;
import com.fivefu.core.camera.vo.response.ProPageList;
import com.fivefu.core.camera.vo.response.ProSourcePageList;
import com.fivefu.core.module.auth.utils.SecurityUtil;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fivefu.base.web.controller.BaseController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 探头表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-19 13:48:32
 */
@Api(tags = "探头管理")
@RestController
@RequestMapping("/db-monitor-camera")
public class DbMonitorCameraController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(DbMonitorCameraController.class);

    @Autowired
    private MonitorCameraService monitorCameraService;

    @ApiOperation(value = "同步探头",httpMethod = "GET",response = ResultInfo.class,notes = "同步探头")
    @RequestMapping(path = "/synCameraInfo",method = RequestMethod.GET)
    public void synCameraInfo(){
        try {
            monitorCameraService.synCameraInfo();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }

    @ApiOperation(value = "同步探头在线状态",httpMethod = "GET",response = ResultInfo.class,notes = "同步探头在线状态")
    @RequestMapping(path = "/synCameraOnlineStatus",method = RequestMethod.GET)
    public void synCameraOnlineStatus(){
        try {
            monitorCameraService.synCameraOnlineStatus();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }

    @ApiOperation(value = "分页查询探头列表",httpMethod = "POST",response = ResultInfo.class,notes = "分页查询探头列表")
    @RequestMapping(path = "/getProbePageList",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "Integer"),

            @ApiImplicitParam(name = "cameraCode", value = "探头编号", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraName", value = "探头名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraType", value = "探头类型", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraManufacturer", value = "厂商", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraAreaid", value = "所属区域", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStreetid", value = "所属街道", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStatus", value = "探头状态", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态", paramType = "query",required = false, dataType = "String"),
    })
    public ResultInfo getProbePageList(Integer page,Integer limit){
        try {
            logger.info("分页查询探头列表接收数据：" + JSON.toJSONString(request.getParameterMap()));
            //判断当前页是否为空
            if (page == null){
                return ResultInfo.renderError("当前页不能为空");
            }
            //判断每页显示条数是否为空
            if (limit == null){
                return ResultInfo.renderError("每页显示条数不能为空");
            }
            String cameraCode = StrUtils.isCheckNull(request.getParameter("cameraCode"));
            String cameraName = StrUtils.isCheckNull(request.getParameter("cameraName"));
            String cameraType = StrUtils.isCheckNull(request.getParameter("cameraType"));
            String cameraManufacturer = StrUtils.isCheckNull(request.getParameter("cameraManufacturer"));
            String cameraAreaid = StrUtils.isCheckNull(request.getParameter("cameraAreaid"));
            String cameraStreetid = StrUtils.isCheckNull(request.getParameter("cameraStreetid"));
            String cameraStatus = StrUtils.isCheckNull(request.getParameter("cameraStatus"));
            String onlineStatus = StrUtils.isCheckNull(request.getParameter("onlineStatus"));

            Page<ProPageList> probePageList = monitorCameraService.getProbePageList(page,limit,cameraCode,cameraName,cameraType,cameraManufacturer,cameraAreaid,cameraStreetid,cameraStatus, onlineStatus);

            return ResultInfo.renderSuccess(0,"success",probePageList.getTotal(),probePageList.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("分页查询探头列表出错");
    }

    @ApiOperation(value = "编辑探头",httpMethod = "POST",response = ResultInfo.class,notes = "编辑探头")
    @RequestMapping(path = "/editProbe",method = RequestMethod.POST)
    public ResultInfo editProbe(@ModelAttribute @Valid DbMonitorCameraVo cameraVo, @RequestAttribute String userid){
        try {
            logger.info("编辑探头接收数据：" + JSON.toJSONString(request.getParameterMap()));
            //校验字段是否缺失
            String value = VerifyDataUtils.verifyRequiredColumn(cameraVo);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }
            //编辑探头
            boolean flag = monitorCameraService.editMonitorCamera(cameraVo, userid);
            if (flag) return ResultInfo.renderSuccess("编辑场景成功");
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("编辑探头失败");
    }

    @ApiOperation(value = "分页查询探头源数据列表",httpMethod = "POST",response = ResultInfo.class,notes = "分页查询探头源数据列表")
    @RequestMapping(path = "/getProbeSourcePageList",method = RequestMethod.POST)
    public ResultInfo getProbeSourcePageList(@ModelAttribute @Valid ProSourcePage proPage){
        try {
            logger.info("分页查询探头源数据列表接收数据：" + JSON.toJSONString(request.getParameterMap()));
            //校验字段是否缺失
            String value = VerifyDataUtils.verifyRequiredColumn(proPage);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }

            Page<ProSourcePageList> probeSourcePageList = monitorCameraService.getProbeSourcePageList(proPage);
            return ResultInfo.renderSuccess(200,"success",probeSourcePageList.getTotal(),probeSourcePageList.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("分页查询探头列表出错");
    }
}
